using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using PlanetenGravity.ScreenSystem;


namespace PlanetenGravity
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Scene : IGameScreen
    {
        Random rnd = new Random();

        List<Planet> planets = new List<Planet>();
        List<Asteroid> asteroids = new List<Asteroid>();

        ContentManager content;
        public Game1 MainGame { get; set; }

        public Scene(Game1 game)
        {
            MainGame = game;
            InputManager.Instance.MouseDown += new MouseClickEventHandler(input_MouseDown);
            InputManager.Instance.KeyDown += new KeyDownEventHandler(Instance_KeyDown);
        }

        void Instance_KeyDown(KeyPressedEventArgs e)
        {
            if (e.Key == Keys.Escape)
                MainGame.SwitchGameScreen(MainGame.mainMenu);
        }

        void input_MouseDown(MouseClickEventArgs e)
        {
            if (e.MouseButton == MouseButton.Left)
            {
                CreateNewAsteroid(e.MousePosition);
            }
        }
        void CreateNewAsteroid(Vector2 position)
        {
            //Random direction
            Vector2 direction = new Vector2(rnd.Next(-100,100), rnd.Next(-100,100));
            direction.Normalize();

            Asteroid a = new Asteroid(content.Load<Texture2D>("Asteroid"), 10, position, direction);
            asteroids.Add(a);
        }
        protected void LoadContent()
        {
            planets.Add(new Planet(content.Load<Texture2D>("Planet"),
                                   500,
                                   new Vector2(100, 100),
                                   Vector2.Zero,
                                   1000,
                                   1));

            planets.Add(new Planet(content.Load<Texture2D>("Planet"),
                                   500,
                                   new Vector2(700, 100),
                                   Vector2.Zero,
                                   1000,
                                   1));

            planets.Add(new Planet(content.Load<Texture2D>("Planet"),
                                   500,
                                   new Vector2(100, 500),
                                   Vector2.Zero,
                                   1000,
                                   1));

            planets.Add(new Planet(content.Load<Texture2D>("Planet"),
                                   500,
                                   new Vector2(700, 500),
                                   Vector2.Zero,
                                   1000,
                                   1));

            asteroids.Add(new Asteroid(content.Load<Texture2D>("Asteroid"),
                          10,
                          new Vector2(390, 290),
                          new Vector2(-1, -3)));

            asteroids.Add(new Asteroid(content.Load<Texture2D>("Asteroid"),
                                       10,
                                       new Vector2(410, 290),
                                       new Vector2(1, -3)));

            asteroids.Add(new Asteroid(content.Load<Texture2D>("Asteroid"),
                                       10,
                                       new Vector2(390, 310),
                                       new Vector2(-1, 3)));

            asteroids.Add(new Asteroid(content.Load<Texture2D>("Asteroid"),
                                       10,
                                       new Vector2(410, 310),
                                       new Vector2(1, 3)));

            asteroids.Add(new Asteroid(content.Load<Texture2D>("Asteroid"),
                                       10,
                                       new Vector2(405, 200),
                                       new Vector2(0, 1)));

            asteroids.Add(new Asteroid(content.Load<Texture2D>("Asteroid"),
                                       10,
                                       new Vector2(200, 295),
                                       new Vector2(1, 0)));

            asteroids.Add(new Asteroid(content.Load<Texture2D>("Asteroid"),
                                       10,
                                       new Vector2(600, 305),
                                       new Vector2(-1, 0)));

            asteroids.Add(new Asteroid(content.Load<Texture2D>("Asteroid"),
                                       10,
                                       new Vector2(395, 400),
                                       new Vector2(0, -1)));

        }
        public void Update(GameTime gameTime)
        {
            foreach (var a in asteroids)
            {
                a.Position += a.Speed;

                foreach (var a2 in asteroids)
                {
                    if (a == a2)
                        continue;
                    Collision<Sprite> collision = new Collision<Sprite>(a, a2);
                }
                foreach (var p in planets)
                {
                    Gravity<Sprite> gravity = new Gravity<Sprite>(p, a);
                    Collision<Sprite> collision = new Collision<Sprite>(a, p);
                }
            }
        }
        public void Draw(SpriteBatch batch, GameTime gameTime)
        {
            batch.Begin();

            var p = from planet in planets
                    select new { planet.Texture, planet.Position, planet.Origin };
            var a = from asteroid in asteroids
                    select new { asteroid.Texture, asteroid.Position, asteroid.Origin };

            foreach (var sprite in p.Concat(a))
            {
                batch.Draw(sprite.Texture, sprite.Position, null, Color.White, 0f, sprite.Origin, 1, SpriteEffects.None, 1);
            }
            batch.End();
        }

        public void Initialize(GraphicsDeviceManager graphics, ContentManager content)
        {
            this.content = content;
            LoadContent();
        }

        public void UnloadContent()
        {
        }
    }
}